import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;


public class SaveLoad {

	private File file;
	private SpaceTraderGui gui;
	
	public SaveLoad(SpaceTraderGui gui) {
		this.gui = gui;
	}

	
	/**
	 * Save the game to a file
	 * Structure of save file:
	 * 
	 */
	public void saveGame() {
		if(file != null) file.delete();
		try {
			file = new File("save.sav");
			FileOutputStream saveFile = new FileOutputStream(file);
			ObjectOutputStream writer = new ObjectOutputStream(saveFile);
			writer.writeObject(this.gui.getPlayer());
			writer.close();
		} catch(Exception e) {
			System.out.println("Could not save file");
			System.out.println(e.toString());
			System.out.println(e.getStackTrace());
		}
	}
	
	public void loadGame() {
		try {
			FileInputStream saveFile = new FileInputStream("save.sav");
			ObjectInputStream reader = new ObjectInputStream(saveFile);
			Player player = (Player) reader.readObject();
			reader.close();
			gui.setPlayer(player);
			gui.updateSolarSystem();
//			gui.updateMarketPlace(player);
//			gui.getChartPanel().setCurrSystem(gui.getCurrentSystem());
//			gui.updateTargetSystem(null);
			gui.getChartPanel().repaint();
		} catch(Exception e) {
			System.out.println("No save file exists");
			System.out.println(e.getMessage());
		}
	}
	
}
